package com.gimi.cloud.jimi.app.consumer.service.user.impl;

import com.gimi.cloud.jimi.app.consumer.common.CheckUtil;
import com.gimi.cloud.jimi.app.consumer.feign.UserInfoFeignService;
import com.gimi.cloud.jimi.app.consumer.service.user.UserInfoService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.bank.BankCardDTO;
import com.gimi.cloud.jimi.dto.userInfo.RdUserInfoDTO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService{
    private static Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);
    @Autowired
    private UserInfoFeignService userInfoFeignService;
	@Override
	public void addUserInfo(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdUserInfoDTO dto = JsonUtil.jsonToBean(requestBody,RdUserInfoDTO.class);
        Util.sendResponse(userInfoFeignService.addUserInfo(dto), response);
	}

	@Override
	public void getUserInfoSingle(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdUserInfoDTO dtoparam = JsonUtil.jsonToBean(requestBody,RdUserInfoDTO.class);
		Util.sendResponse(userInfoFeignService.getUserInfoSingle(dtoparam), response);
	}

	@Override
	public void updateUserInfo(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdUserInfoDTO dto = JsonUtil.jsonToBean(requestBody,RdUserInfoDTO.class);
		Util.sendResponse(userInfoFeignService.updateUserInfo(dto), response);
	}

	@Override
	public void uploadCardPhoto(HttpServletResponse response, HttpServletRequest request) {
		StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
		MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
		Map<String,String> map = new HashMap<>();
		String type = request.getParameter("type");
		String userId = request.getHeader("userId");
		if(StringUtil.isBlank(type) || StringUtil.isBlank(userId)){
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		logger.info(String.format("===>userId = %s type = %s",userId,type));
		map.put("type",type);
		map.put("userId",userId);

		Util.sendResponse(userInfoFeignService.uploadCardPhoto(multipartFiles,map),response);
	}

	@Override
	public void updateUserIdInfo(String requestData, HttpServletResponse response, HttpServletRequest request) {
		logger.info("身份证更新,传入参数srcData:" + requestData);
		String userId = request.getHeader("userId");

		RdUserInfoDTO rdUserInfoDTO = JsonUtil.jsonToBean(requestData,RdUserInfoDTO.class);

		//校验参数非空
		if(StringUtil.isBlank(rdUserInfoDTO.getValidDate()) || StringUtil.isBlank(rdUserInfoDTO.getSex()) || StringUtil.isBlank(rdUserInfoDTO.getRealName())||
			StringUtil.isBlank(rdUserInfoDTO.getNationality()) || StringUtil.isBlank(rdUserInfoDTO.getBirthday()) ||
			StringUtil.isBlank(rdUserInfoDTO.getAddress()) || StringUtil.isBlank(rdUserInfoDTO.getPolice()) ||
			StringUtil.isBlank(rdUserInfoDTO.getCardId()) ||StringUtil.isBlank(rdUserInfoDTO.getHomeAddress())||StringUtil.isBlank(rdUserInfoDTO.getCardFront()) || StringUtil.isBlank(rdUserInfoDTO.getCardBack()) || StringUtil.isBlank(rdUserInfoDTO.getAvatar())){
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}

		// 检查身份证有效时间
		CheckUtil.checkValidDate(rdUserInfoDTO.getValidDate(), response);

		rdUserInfoDTO.setUserId(Integer.parseInt(userId));
		// 获得IP地址
		//responseDataMap.put("IP", IPUtil.getRemortIP(request));
		Util.sendResponse(userInfoFeignService.updateAppUserIdinfo(rdUserInfoDTO), response);
	}

	@Override
	public void getUserInfo(String requestData, HttpServletResponse response, HttpServletRequest request) {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestData));
		String userId = request.getHeader("userId");
		if (StringUtil.isBlank(userId)) {//验证参数合法性
			logger.error("参数错误");
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		RdUserInfoDTO dto = new RdUserInfoDTO();
		dto.setUserId(Integer.parseInt(userId));

		Util.sendResponse(userInfoFeignService.getUserInfo(dto), response);
	}

	@Override
	public void updateUserbankcard(String requestData, HttpServletResponse response, HttpServletRequest request) {
		logger.info("身份证更新,传入参数srcData:" + requestData);
		String userId = request.getHeader("userId");
		// 校验用户ID
		CheckUtil.checkUserId(userId, response);

		BankCardDTO bankCardDTO = JsonUtil.jsonToBean(requestData,BankCardDTO.class);
		bankCardDTO.setUserId(Integer.parseInt(userId));
		if(StringUtil.isBlank(bankCardDTO.getCardNo()) || StringUtil.isBlank(bankCardDTO.getCity())|| StringUtil.isBlank(bankCardDTO.getCounty())
				|| StringUtil.isBlank(bankCardDTO.getNation()) || StringUtil.isBlank(bankCardDTO.getPhone()) || StringUtil.isBlank(bankCardDTO.getCounty())
				|| StringUtil.isBlank(bankCardDTO.getUserName()) || StringUtil.isBlank(bankCardDTO.getSubBankName())){
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}

		Util.sendResponse(userInfoFeignService.updateUserbankcard(bankCardDTO), response);
	}

	@Override
	public void bankCardSign(String requestData, HttpServletResponse response, HttpServletRequest request) {
		logger.info("身份证更新,传入参数srcData:" + requestData);
		String userId = request.getHeader("userId");
		// 校验用户ID
		CheckUtil.checkUserId(userId, response);
		BankCardDTO bankCardDTO = JsonUtil.jsonToBean(requestData,BankCardDTO.class);
		bankCardDTO.setUserId(Integer.parseInt(userId));
		if (StringUtil.isEmpty(userId) || "0".equals(userId) || StringUtil.isEmpty(bankCardDTO.getCardNo())) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		Util.sendResponse(userInfoFeignService.bankCardSign(bankCardDTO), response);
	}

	@Override
	public void getUserBankcard(String requestData, HttpServletResponse response, HttpServletRequest request) {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestData));
		String userId = request.getHeader("userId");
		if (StringUtil.isBlank(userId)) {//验证参数合法性
			logger.error("参数错误");
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		RdUserInfoDTO dto = new RdUserInfoDTO();
		dto.setUserId(Integer.parseInt(userId));

		Util.sendResponse(userInfoFeignService.getUserBankcard(dto), response);
	}

}
